<?php
/**
 * Facebook 面试题之 判断四个点能否组成正方形
 */

class Square
{
    protected $point = [];
    
    public function __construct(array $point)
    {
        $this->point = $point;
    }
    
    public function check()
    {
        $result = [];
        if (count($this->point) != 4)
        {
            return false;
        }
        
        $count = count($this->point);
        for ($i = 0; $i < $count; $i++)
        {
            for ($j = $i + 1; $j < $count; $j++)
            {
                $result[]=$this->_calculation($i,$j);
            }
        }
        sort($result);
        
        if ($result[0] == $result[1] && $result[4] == $result[5] && $result[4] > $result[1]) {
            return true;
        }
        return false;
        
    }
    
    private function _calculation($i, $j)
    {
        return ($this->point[$i][0] - $this->point[$j][0]) * ($this->point[$i][0] - $this->point[$j][0]) + ($this->point[$i][1] - $this->point[$j][1]) *
               ($this->point[$i][1] - $this->point[$j][1]);
    }
}

$obj = new Square([[0, 0], [1, 0], [1, 1], [0, 1]]);
var_dump($obj->check());